﻿using System;
using System.Globalization;
using System.Text;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Web.Common;
using Resources;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class HopDongExport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var donVi = Request.Params["donVi"];
            var soHopDong = Request.Params["soHopDong"];
            var thiCongXong = Request.Params["thiCongXong"] == "true";
            var tenHopDong = Request.Params["tenHopDong"];
            var ngayQuyetToanFrom = Request.Params["ngayQuyetToanFrom"];
            var ngayQuyetToanTo = Request.Params["ngayQuyetToanTo"];
            int trangThaiHopDongId;
            int.TryParse(Request.Params["trangThaiHopDongId"], out trangThaiHopDongId);
            var ngayHoanThienFrom = Request.Params["ngayHoanThienFrom"];
            var ngayHoanThienTo = Request.Params["ngayHoanThienTo"];
            int employeeId;
            int.TryParse(Request.Params["employeeId"], out employeeId);

            DateTime? dtHoanThienFrom = null, dtHoanThienTo = null, dtQuyetToanFrom = null, dtQuyetToanTo = null;
            if (trangThaiHopDongId == 5)
            {
                if (!string.IsNullOrEmpty(ngayHoanThienFrom))
                {
                    DateTime dt;
                    if (DateTime.TryParseExact(ngayHoanThienFrom, GlobalStrings.DateFormat,
                        CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                    {
                        dtHoanThienFrom = dt;
                    }
                }
                if (!string.IsNullOrEmpty(ngayHoanThienTo))
                {
                    DateTime dt;
                    if (DateTime.TryParseExact(ngayHoanThienTo, GlobalStrings.DateFormat,
                        CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                    {
                        dtHoanThienTo = dt;
                    }
                }
            }
            if (!string.IsNullOrEmpty(ngayQuyetToanFrom))
            {
                DateTime dt;
                if (DateTime.TryParseExact(ngayQuyetToanFrom, GlobalStrings.DateFormat,
                    CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                {
                    dtQuyetToanFrom = dt;
                }
            }
            if (!string.IsNullOrEmpty(ngayQuyetToanTo))
            {
                DateTime dt;
                if (DateTime.TryParseExact(ngayQuyetToanTo, GlobalStrings.DateFormat,
                    CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                {
                    dtQuyetToanTo = dt;
                }
            }

            var html = GenerateHtml(soHopDong, tenHopDong, donVi, dtHoanThienFrom, dtHoanThienTo, dtQuyetToanFrom, dtQuyetToanTo, trangThaiHopDongId, thiCongXong, employeeId);
            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition",
                string.Format("attachment;filename=Hop_Dong_{0:dd-MM-yyyy_hhmmss}.xls", DateTime.Now));
            Response.ContentEncoding = Encoding.UTF8;
            Response.ContentType = "application/vnd.ms-excel";
            Response.BinaryWrite(Encoding.Unicode.GetPreamble());
            var bytes = Encoding.Unicode.GetBytes(html);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }

        private static string GenerateHtml(string soHopDong, string tenHopDong, string tenDonVi,
            DateTime? ngayHoanThienFrom, DateTime? ngayHoanThienTo,
            DateTime? ngayQuyetToanFrom, DateTime? ngayQuyetToanTo,
            int trangThai, bool thiCongXong, int employeeId)
        {
            var hopDongs = new HopDongBiz().ExportHopDongs(soHopDong, tenHopDong, tenDonVi,
                ngayHoanThienFrom, ngayHoanThienTo, ngayQuyetToanFrom, ngayQuyetToanTo, trangThai, thiCongXong, WebCommon.CurrrentEmployee,
                employeeId);

            var headers = new[]
            {
                "Số HĐ", "Tên HĐ", "Đơn vị", "Ngày ký", "Ngày hoàn thiện", "Ngày quyết toán", "Tình trạng HĐ"
            };

            var sbHtml = new StringBuilder();
            sbHtml.Append("<html><body style='font-size:12px !important;font-family:Times New Roman !important;'><head><meta charset=\"utf-8\" />");
            sbHtml.Append("<style type='text/css'>td {vertical-align:top;}</style>");
            sbHtml.Append("</head><body>");
            sbHtml.Append("<table cellpadding='0' cellspacing='0' width='100%' style='border-collapse:collapse;'>");

            // headers
            sbHtml.Append("<tr>");
            foreach (var header in headers)
            {
                sbHtml.AppendFormat("<td style='border:solid 0.1pt; text-align:center;'><strong>{0}</strong></td>", header);
            }
            sbHtml.Append("</tr>");

            const string formatText = "<td style='border:solid 0.1pt;'>{0}</td>";
            const string formatTextLeft = "<td style='border:solid 0.1pt; text-align:left;'>{0}</td>";
            const string formatDate = "<td style='border:solid 0.1pt; text-align:right;'>{0}</td>";
            foreach (var hopDong in hopDongs)
            {
                sbHtml.Append("<tr>");
                sbHtml.AppendFormat(formatTextLeft, hopDong.SoHopDong);
                sbHtml.AppendFormat(formatText, hopDong.TenHopDong);
                sbHtml.AppendFormat(formatText, hopDong.TenDonVi);
                sbHtml.AppendFormat(formatDate, hopDong.NgayKy.HasValue ? hopDong.NgayKy.Value.ToString(GlobalStrings.DateFormat) : string.Empty);
                sbHtml.AppendFormat(formatDate, hopDong.NgayHoanThien.HasValue ? hopDong.NgayHoanThien.Value.ToString(GlobalStrings.DateFormat) : string.Empty);
                sbHtml.AppendFormat(formatDate, hopDong.NgayQuyetToan.HasValue ? hopDong.NgayQuyetToan.Value.ToString(GlobalStrings.DateFormat) : string.Empty);
                sbHtml.AppendFormat(formatText, hopDong.TinhTrangHopDong);
                sbHtml.Append("</tr>");
            }

            sbHtml.Append("</table>");
            sbHtml.Append("</body></html>");

            return sbHtml.ToString();
        }
    }
}